<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>


	<xsl:template name="FormatPhone">
		<xsl:param name="FullPhoneNumber"/>
		
		<xsl:text>(</xsl:text>
		<xsl:value-of select="substring($FullPhoneNumber,1,3)"/>
		<xsl:text>) </xsl:text>
		<xsl:value-of select="substring($FullPhoneNumber,4,3)"/>
		<xsl:text>-</xsl:text>
		<xsl:value-of select="substring($FullPhoneNumber,7,4)"/>
	</xsl:template>
	
<xsl:template name="FormatDate">
		<xsl:param name="FullDate"/>
		
		<xsl:value-of select="substring($FullDate,6,2)"/>
		<xsl:text>/</xsl:text>
		<xsl:value-of select="substring($FullDate,9,2)"/>
		<xsl:text>/</xsl:text>
		<xsl:value-of select="substring($FullDate,1,4)"/>
	</xsl:template>
	
<xsl:template name="FormatSSN">
		<xsl:param name="FullSSN"/>
		
		<xsl:value-of select="substring($FullSSN,1,3)"/>
		<xsl:text>-</xsl:text>
		<xsl:value-of select="substring($FullSSN,4,2)"/>
		<xsl:text>-</xsl:text>
		<xsl:value-of select="substring($FullSSN,6,4)"/>
	</xsl:template>
	
	<xsl:template name="pad">
		<xsl:param name="amount"/>
		<xsl:param name="data"/>


		<xsl:if test="string-length($data) > $amount">
			<xsl:value-of select="substring($data,1,$amount)"/>
		</xsl:if>
		<xsl:if test="string-length($data) = $amount">
			<xsl:value-of select="$data"/>
		</xsl:if>
		<xsl:if test="string-length($data) &lt; $amount">
			<xsl:value-of select="$data"/>
		
			<xsl:call-template name="spaceover">
				<xsl:with-param name="amount" select="$amount"/>
				<xsl:with-param name="amountWrote" select="string-length($data)"/>
			
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="spaceover">
		<!--
	**************************************************************************************************
	|| Adds a given number of spaces to the output. This template will write out (return) a given 
	||   number of spaces. Chage the amount to the desired number of spaces you wish to write out.
	|| Input:
	||   amount      - Number of spaces to write out.
	||   amountWrote - Hold Variable: Used to signafy amount wrote, start with 0.
	|| Example Call:
	||   <xsl:call-template name="spaceover">
	||	 	<xsl:with-param name="amount" select="10"/>
	||		<xsl:with-param name="amountWrote" select="0"/>
	||	 </xsl:call-template>
	**************************************************************************************************
	-->
		<xsl:param name="amount"/>
		<xsl:param name="amountWrote"/>
		<xsl:if test="$amountWrote &lt; $amount">
			<xsl:text> </xsl:text>
			<xsl:call-template name="spaceover">
				<xsl:with-param name="amount" select="$amount"/>
				<xsl:with-param name="amountWrote" select="$amountWrote + 1"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	
		<xsl:template name="wrapIn">
		<!--
**************************************************************************************************
|| The below template wraps a text string at a specified position given a specified Start and
||  end position.
|| Input:
||   Text           - The inputted text to wrap: NOTE: perform a normalize-string() before to
||                     strip extra spaces and carriage returns.
||   StartPos       - The start position of where the string will start writing from.
||   EndPos         - The end position of where the string will stop writing and begin a wrap.
||   WrapedStartPos - The wrapped start position.  Where to start writing on the wrapped line.
||   CurrentPos     - Current postion (used internally).
||   WrapWidth      - The wrap width.
||   initialRun     - Initial Run (used internally).  Identifies if it is the first run of the
||                     template.  If it is the first run, the amount to space over before writing
||                     can and will be different so this parameter adjusts accordingly.
|| Explanation/Notes:
||   As mentioned in the input section, perform a normalize-string on the inputted text to remove
||    unwanted extra spaces or carriage returns.
|| Example Call:
||   <xsl:call-template name="wrapIn">
||		<xsl:with-param name="Text" select="normalize-space(./example/comments)"/>
||		<xsl:with-param name="StartPos" select="$posCol2"/>
||		<xsl:with-param name="EndPos" select="$posMaxWidth"/>
||		<xsl:with-param name="WrapedStartPos" select="$posCol2"/>
||		<xsl:with-param name="CurrentPos" select="$posMaxWidth + 1"/>
||		<xsl:with-param name="WrapWidth" select="$posMaxWidth + 1"/>
||		<xsl:with-param name="initialRun" select="0"/>
||	</xsl:call-template>
**************************************************************************************************
-->
		<xsl:param name="Text"/>
		<xsl:param name="StartPos"/>
		<xsl:param name="EndPos"/>
		<xsl:param name="WrapedStartPos"/>
		<xsl:param name="CurrentPos"/>
		<xsl:param name="WrapWidth"/>
		<xsl:param name="initialRun"/>
		<xsl:variable name="l" select="$EndPos - $WrapedStartPos"/>
		<xsl:variable name="n-text" select="normalize-space($Text)"/>
		<xsl:choose>
			<xsl:when test="string-length($Text) &lt;= $l">
				<!-- if passed in string is lt or = the length we can write it -->
				<xsl:choose>
					<!-- Determine the correct spacing before writing -->
					<xsl:when test="$initialRun = 0">
						<!-- 0=True -->
						<xsl:call-template name="spaceover">
							<xsl:with-param name="amount" select="$StartPos"/>
							<!-- use StartPos (adjusted started position) -->
							<xsl:with-param name="amountWrote" select="0"/>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="spaceover">
							<xsl:with-param name="amount" select="$WrapedStartPos"/>
							<!-- if not initial run then it is a wrapped line -->
							<xsl:with-param name="amountWrote" select="0"/>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:value-of select="$Text"/>
				<!-- Write the string -->
			</xsl:when>
			<xsl:otherwise>
				<!-- else wrap the string -->
				<xsl:choose>
					<xsl:when test="$CurrentPos &lt; 1">
						<!-- If CurrentPos = 0 it means the word is longer than the wrap width
				      so output as much as possible on one line, then pass the
				      remainder of the string on for more processing. -->
						<xsl:text>&#10;</xsl:text>
						<xsl:call-template name="wrapIn">
							<xsl:with-param name="Text" select="substring($Text,$WrapWidth)"/>
							<xsl:with-param name="StartPos" select="$StartPos"/>
							<xsl:with-param name="EndPos" select="$EndPos"/>
							<xsl:with-param name="WrapedStartPos" select="$WrapedStartPos"/>
							<xsl:with-param name="CurrentPos" select="$WrapWidth"/>
							<xsl:with-param name="WrapWidth" select="$WrapWidth"/>
							<xsl:with-param name="initialRun" select="1"/>
						</xsl:call-template>
					</xsl:when>
					<xsl:when test="(string-length($Text) + $WrapedStartPos) > ($CurrentPos - 1)">
						<!-- if input string length is greater than 72 wrap it up -->
						<xsl:choose>
							<xsl:when test="substring($Text, (($CurrentPos - 1)- $WrapedStartPos), 1) = ' '">
								<!-- if the last character in the string is a [space] then write the line else move back and retry -->
								<xsl:choose>
									<!-- get the correct spacing -->
									<xsl:when test="$initialRun = 0">
										<!-- 0=True -->
										<xsl:call-template name="spaceover">
											<xsl:with-param name="amount" select="$StartPos"/>
											<!-- use StartPos (adjusted started position) -->
											<xsl:with-param name="amountWrote" select="0"/>
										</xsl:call-template>
									</xsl:when>
									<xsl:otherwise>
										<xsl:call-template name="spaceover">
											<xsl:with-param name="amount" select="$WrapedStartPos"/>
											<!-- if not initial run then it is a wrapped line -->
											<xsl:with-param name="amountWrote" select="0"/>
										</xsl:call-template>
									</xsl:otherwise>
								</xsl:choose>
								<xsl:value-of select="substring($Text, 1, ($CurrentPos - 1) - $WrapedStartPos)"/>
								<xsl:text>&#10;</xsl:text>
								<xsl:call-template name="wrapIn">
									<xsl:with-param name="Text" select="substring($Text,$CurrentPos - $WrapedStartPos)"/>
									<xsl:with-param name="StartPos" select="$StartPos"/>
									<xsl:with-param name="EndPos" select="$EndPos"/>
									<xsl:with-param name="WrapedStartPos" select="$WrapedStartPos"/>
									<xsl:with-param name="CurrentPos" select="$WrapWidth"/>
									<xsl:with-param name="WrapWidth" select="$WrapWidth"/>
									<xsl:with-param name="initialRun" select="1"/>
									<!-- set to 1 as we wrote the above line -->
								</xsl:call-template>
							</xsl:when>
							<xsl:otherwise>
								<!-- go back a character and reEvaluate -->
								<xsl:call-template name="wrapIn">
									<xsl:with-param name="Text" select="$Text"/>
									<xsl:with-param name="StartPos" select="$StartPos"/>
									<xsl:with-param name="EndPos" select="$EndPos"/>
									<xsl:with-param name="WrapedStartPos" select="$WrapedStartPos"/>
									<xsl:with-param name="CurrentPos" select="$CurrentPos - 1"/>
									<xsl:with-param name="WrapWidth" select="$WrapWidth"/>
									<xsl:with-param name="initialRun" select="$initialRun"/>
									<!-- leave at inputted value as we have not wrote a line -->
								</xsl:call-template>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$Text"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
